package com.biz.primus.app.erpbridge.service;

import com.biz.primus.app.erpbridge.feign.GeoFeignClient;
import com.biz.primus.app.erpbridge.vo.geo.GeoPushResp;
import com.biz.primus.base.constant.GeoStreamConstant;
import com.biz.primus.model.depot.vo.GeoAllMessageVo;
import com.biz.primus.model.geo.enums.GeoLevel;
import com.biz.primus.model.geo.vo.SimpleCityVo;
import com.biz.primus.model.geo.vo.SimpleDistrictVo;
import com.biz.primus.model.geo.vo.SimpleProvinceVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * GeoPushService
 *
 * @author JKLiues
 * @date 2017年11月17日
 */
@Slf4j
@Service
public class GeoPushService {

    @Value("${primus.push.geo.province}")
    private String provincePushUrl;

    @Value("${primus.push.geo.city}")
    private String cityPushUrl;

    @Value("${primus.push.geo.district}")
    private String districtPushUrl;

    @Autowired
    private GeoFeignClient geoFeignClient;

    @Autowired
    private RestTemplate restTemplate;

    @StreamListener(GeoStreamConstant.ALL)
    public void process(GeoAllMessageVo vo) {
        StopWatch stopWatch = new StopWatch("数据推送服务开启");
        stopWatch.start("推送数据");
        this.handle();
        stopWatch.stop();
    }

    public void handle() {
        this.pushProvince();
        this.pushCity();
        this.pushDistrict();
    }

    @StreamListener(GeoStreamConstant.PROVINCE)
    public void pushProvince(SimpleProvinceVo vo) {
        GeoPushResp geoPushResp = restTemplate.postForObject(provincePushUrl, Lists.newArrayList(vo), GeoPushResp.class);
        geoFeignClient.doPushLog(1, GeoLevel.GEO_PROVINCE, geoPushResp.getCode());
    }

    @StreamListener(GeoStreamConstant.CITY)
    public void pushCity(SimpleCityVo vo) {
        GeoPushResp geoPushResp = restTemplate.postForObject(cityPushUrl, Lists.newArrayList(vo), GeoPushResp.class);
        geoFeignClient.doPushLog(1, GeoLevel.GEO_CITY, geoPushResp.getCode());
    }

    @StreamListener(GeoStreamConstant.DISTRICT)
    public void pushDistrict(SimpleDistrictVo vo) {
        GeoPushResp geoPushResp = restTemplate.postForObject(districtPushUrl, Lists.newArrayList(vo), GeoPushResp.class);
        geoFeignClient.doPushLog(1, GeoLevel.GEO_DISTRICT, geoPushResp.getCode());
    }

    private void pushProvince() {
        List<SimpleProvinceVo> simpleProvinceVos = geoFeignClient.buildPushProvince();
        GeoPushResp geoPushResp = restTemplate.postForObject(provincePushUrl, simpleProvinceVos, GeoPushResp.class);
        geoFeignClient.doPushLog(simpleProvinceVos.size(), GeoLevel.GEO_PROVINCE, geoPushResp.getCode());
    }

    private void pushCity() {
        List<SimpleProvinceVo> simpleProvinceVos = geoFeignClient.buildPushProvince();
        GeoPushResp geoPushResp = restTemplate.postForObject(provincePushUrl, simpleProvinceVos, GeoPushResp.class);
        geoFeignClient.doPushLog(simpleProvinceVos.size(), GeoLevel.GEO_CITY, geoPushResp.getCode());
    }

    private void pushDistrict() {
        List<SimpleProvinceVo> simpleProvinceVos = geoFeignClient.buildPushProvince();
        GeoPushResp geoPushResp = restTemplate.postForObject(provincePushUrl, simpleProvinceVos, GeoPushResp.class);
        geoFeignClient.doPushLog(simpleProvinceVos.size(), GeoLevel.GEO_DISTRICT, geoPushResp.getCode());
    }
}